package edu.arizona.sista.learn.model.motion;

import java.io.File;

import com.martiansoftware.jsap.FlaggedOption;
import com.martiansoftware.jsap.JSAP;
import com.martiansoftware.jsap.JSAPException;
import com.martiansoftware.jsap.JSAPResult;
import com.martiansoftware.jsap.Switch;

import edu.arizona.sista.learn.model.holding.Scene;
import edu.arizona.sista.learn.util.Utils;
import edu.arizona.sista.learn.util.Utils.MovieName;

public class Main {
	public static void main(String[] args) throws JSAPException {

		JSAP jsap = new JSAP();
		Switch helpSwitch = new Switch("help")
			.setShortFlag('h')
			.setLongFlag("help");
		FlaggedOption tracks = new FlaggedOption("3d-track-file")
			.setDefault("")
			.setRequired(false)
			.setLongFlag("3d-track-file");
		FlaggedOption output = new FlaggedOption("output-file")
			.setDefault("")
			.setRequired(true)
			.setLongFlag("output-file");

		jsap.registerParameter(helpSwitch);
		jsap.registerParameter(tracks);
		jsap.registerParameter(output);

		JSAPResult config = jsap.parse(args);
		if (config.getBoolean("help") || "".equals(config.getString("3d-track-folder")) || "".equals(config.getString("output-file"))) {
			System.out.println(jsap.getUsage());
			return;
		}
		File trackFile = new File(config.getString("3d-track-file"));
		if (!trackFile.isFile()) {
			System.err.println("File not found or invalid!");
			return;
		}

		getActivities(trackFile, config.getString("output-file"));
	}

	private static void getActivities(File trackFile, String output) {
		//Name of the scene: only useful to get actors names -> dependent on the folder of files
		MovieName sceneName = Scene.getName(trackFile);
		Scene scene = new Scene(sceneName);
		
		//Load data
		scene.loadTracks(trackFile);

		//rectilinear trajectories
		RectilinearTrajectory.getRectilinearTrajectories(scene);
		
		//Movement events
		boolean handHeld = (sceneName.equals(MovieName.Newspaper) || sceneName.equals(MovieName.Map));
		Speed.speed(scene, handHeld);

		
		StringBuilder sb = new StringBuilder();
		sb.append(scene.toString());
		Utils.toFile(sb, output);
		//System.out.println(scene);
	}
}
